﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
// add
using System.IO;
using System.Data.SqlServerCe;


namespace FormBase
{
    public partial class FPGM0130 : Form
    {
        public static string msFileName;
        public static SqlCeConnection mConnection;
        SqlCeDataAdapter da = null;
        DataSet ds = null;

        public static SqlCeConnection CreateConnection(string filename)
        {
            //SqlCeConnection con = new SqlCeConnection(@"Data Source=|DataDirectory|\" + filename + ".sdf;Password=123");
            SqlCeConnection con = new SqlCeConnection(@"Data Source=|DataDirectory|\App_Data\" + filename + ".sdf;Password=123");
            //SqlCeConnection con = new SqlCeConnection(@"Data Source=|DataDirectory|\App_Data\" + filename + ".sdf;");
            if ((mConnection != null) && (mConnection.State == System.Data.ConnectionState.Open)) 
                mConnection.Close();

            mConnection = con;
            mConnection.Open();
            msFileName = filename;
            return con;
        }

        public FPGM0130()
        {
            InitializeComponent();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void FPGM0130_Load(object sender, EventArgs e)
        {
            //textBox2.Text = "程式請參考: SampleSqlCE(in v090目錄中). 注意需要: .NET Framework 3.5 SP1 或 .NET Framework 4 以用於 Managed 應用程式開發. 若在.net 2.0可考慮改用mariaDB.";
        }

        private void btnCreateNewDBName_Click(object sender, EventArgs e)
        {
            string s1, s2;
            if (txtNewDBName.Text != String.Empty)
            {
                s1 = @"App_Data\Model.sdf";
                s2 = @"App_Data\" + txtNewDBName.Text + ".sdf";
                File.Copy(s1, s2);
                FPGM0130.CreateConnection(txtNewDBName.Text);
                MessageBox.Show("File created ");
            }
            //this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            da.Update(ds.Tables[0]);
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            SqlCeCommand comm = new SqlCeCommand("Select * from EmployeeMaster", FPGM0130.mConnection);
            da = new SqlCeDataAdapter(comm);
            da.SelectCommand = comm;
            SqlCeCommandBuilder commb = new SqlCeCommandBuilder(da);
            commb.GetInsertCommand();
            commb.GetUpdateCommand();
            ds = new DataSet();
            da.Fill(ds);

            gvEmpMaster.DataSource = ds.Tables[0];

        }
        /// <summary>
        /// This Method is for sample purpose ,It has not implementations
        /// </summary>
        private void RetriveData()
        {
            string FileName = "Model.sdf";
            SqlCeConnection con = new SqlCeConnection(@"Data Source=|DataDirectory|\" + FileName + ".sdf;Password=123");
            con.Open();
            SqlCeCommand comm = new SqlCeCommand("Select * from EmployeeMaster", con);
            SqlCeDataAdapter sqlceda = new SqlCeDataAdapter(comm);
            DataSet dsData = new DataSet("EMPLOYEE");
            sqlceda.Fill(dsData);
            gvEmpMaster.DataSource = ds.Tables[0];
        }
    }
}
